import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import api from '../../utils/api';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    optionList: [],
    optionListAll: [],
    nextText: '下一题',
    prevText: '上一题',
    proNum:0,
    movableViewInfo: {
      y: 0,
      showClass: 'none',
      data: {}
    },
    pageInfo: {
      rowHeight: 47,
      scrollHeight: 90,

      startIndex: null,
      scrollY: true,
      readyPlaceIndex: null,
      startY: 0,
      selectedIndex: null,
    },
    selectId:0
  },

  dragStart: function (event) {
    var startIndex = event.target.dataset.index
    console.log(startIndex)
    console.log('获取到的元素为', this.data.optionList[startIndex])
    // 初始化页面数据
    var pageInfo = this.data.pageInfo
    pageInfo.startY = event.touches[0].clientY
    pageInfo.readyPlaceIndex = startIndex
    pageInfo.selectedIndex = startIndex
    pageInfo.scrollY = false
    pageInfo.startIndex = startIndex

    this.setData({
      'movableViewInfo.y': pageInfo.startY - (pageInfo.rowHeight / 2)
    })
    // 初始化拖动控件数据
    var movableViewInfo = this.data.movableViewInfo
    movableViewInfo.data = this.data.optionList[startIndex].question
    movableViewInfo.showClass = "inline"

    this.setData({
      movableViewInfo: movableViewInfo,
      pageInfo: pageInfo
    })
  },

  dragMove: function (event) {
    var optionList = this.data.optionList
    var pageInfo = this.data.pageInfo
    // 计算拖拽距离
    var movableViewInfo = this.data.movableViewInfo
    var movedDistance = event.touches[0].clientY - pageInfo.startY
    movableViewInfo.y = pageInfo.startY - (pageInfo.rowHeight / 2) + movedDistance
    // console.log('移动的距离为', movedDistance)

    // 修改预计放置位置
    var movedIndex = parseInt(movedDistance / pageInfo.rowHeight)
    var readyPlaceIndex = pageInfo.startIndex + movedIndex
    if (readyPlaceIndex < 0) {
      readyPlaceIndex = 0
    }
    else if (readyPlaceIndex >= optionList.length) {
      readyPlaceIndex = optionList.length - 1
    }

    if (readyPlaceIndex != pageInfo.selectedIndex) {
      var selectedData = optionList[pageInfo.selectedIndex]

      optionList.splice(pageInfo.selectedIndex, 1)
      optionList.splice(readyPlaceIndex, 0, selectedData)
      pageInfo.selectedIndex = readyPlaceIndex
    }
    // 移动movableView
    pageInfo.readyPlaceIndex = readyPlaceIndex
    // console.log('移动到了索引', readyPlaceIndex, '选项为', optionList[readyPlaceIndex])
    this.setData({
      movableViewInfo: movableViewInfo,
      optionList: optionList,
      pageInfo: pageInfo
    })
  },

  dragEnd: function (event) {
    // 重置页面数据
    var pageInfo = this.data.pageInfo
    pageInfo.readyPlaceIndex = null
    pageInfo.startY = null
    pageInfo.selectedIndex = null
    pageInfo.startIndex = null
    pageInfo.scrollY = true
    // 隐藏movableView
    var movableViewInfo = this.data.movableViewInfo
    movableViewInfo.showClass = 'none'
    this.setData({
      pageInfo: pageInfo,
      movableViewInfo: movableViewInfo
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.data.selectId = options.selectId;
    api.get("evaluation/"+options.selectId, {
    }).then(res => {
      console.log(res)
      if(res.data.length > 0){
        _this.setData({
          optionListLength : res.data.length,
          optionList: res.data[this.data.proNum].item,
          optionListAll:res.data
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  next: function (e) {
    let _this = this;
    let proNum = parseInt(e.target.dataset.pronum);
    if (_this.data.nextText != '提交'){
      console.log(_this.data.optionList)
        _this.postAnswer(_this.data.selectId,_this.data.optionListAll[proNum].id,_this.addList(_this.data.optionList));
        proNum = proNum + 1;
        console.log("======="+proNum)
        let optionList = _this.data.optionListAll[proNum]
        _this.setData({
          proNum: proNum,
          optionList: optionList.item,
        })
        console.log(_this.data.optionListAll.length)
        if (proNum ==  _this.data.optionListAll.length-1) {
          _this.setData({
            nextText: '提交'
          })
        }
      }else{
        Dialog.confirm({
          title: '提示',
          message: '请确认是否提交？'
        }).then(() => {
          console.log(_this.data.selectId)
          _this.postAnswer(_this.data.selectId,_this.data.optionListAll[proNum].id,_this.addList(_this.data.optionList));
          wx.redirectTo({
            url: '../component/pages/mdjzg/mdjzg',
          })
        }).catch(() => {
          console.log('取消')
        });
      }
  },
  prev: function (e) {
    let proNum = parseInt(e.target.dataset.pronum);
    proNum = proNum - 1;
    if (proNum < 0) {
      Notify({ type: 'primary', message: '已是第一题' });
      return;
    }
    this.setData({
      nextText: '下一步',
      proNum: proNum,
      optionList: this.data.optionListAll[proNum].item
    })
  },
/**
   * 答案提交
   */
  postAnswer:function(_evaluation_id,_question_id,_answer){
    let token = wx.getStorageSync('token');
    api.post("answer", {
      "token":token,
      "evaluation_id":_evaluation_id,
      "question_id":_question_id,
      "answer":_answer,
    }).then(res => {
      console.log(res)
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  /**
   * 循环组装数组
   */
  addList:function(list){
    let arr = [];
    for(var i in list){
      arr.push(list[i].id)
    }
    console.log(arr)
    return JSON.stringify(arr);
  },
})